package com.jiazhong.mybatis.m2;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.commons.bean.Emp;
import com.jiazhong.mybatis.m2.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class App {

    private static void a() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.findAll();
        emps.forEach(System.out::println);
        sqlSession.close();
    }

    private static void b() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.findById(7980);
        System.out.println(emp);
        sqlSession.close();
    }

    private static void c() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.findByName("%李%");
        emps.forEach(System.out::println);
        sqlSession.close();
    }

    private static void d() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.findByEnameAndJob("%李%", "程序员");
        emps.forEach(System.out::println);
        sqlSession.close();
    }

    private static void e() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        PageHelper.startPage(1, 10);
        List<Emp> emps = empMapper.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);
        System.out.println(pageInfo);
        sqlSession.close();
    }


    private static void f() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setComm(989.0);
        emp.setSal(8765.2);
        emp.setJob("歌手");
        emp.setMgr(7998);
        emp.setEname("高宇凡星");
        emp.setDeptNo(30);
        empMapper.insert(emp);
        sqlSession.close();
    }

    private static void g() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEmpNo(8048);
        emp.setComm(989.0);
        emp.setSal(8365.2);
        emp.setJob("歌手");
        emp.setMgr(7998);
        emp.setEname("马浩伟");
        emp.setDeptNo(30);
        empMapper.update(emp);
        sqlSession.close();
    }

    private static void h() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        empMapper.delete(8041);
        sqlSession.close();
    }

    public static void main(String[] args) {
        h();
    }
}